package org.jeecg.modules.renygjxl.service.impl;

import org.jeecg.modules.renygjxl.entity.Renygjxl;
import org.jeecg.modules.renygjxl.entity.RenygjxlGuaid;
import org.jeecg.modules.renygjxl.mapper.RenygjxlGuaidMapper;
import org.jeecg.modules.renygjxl.mapper.RenygjxlMapper;
import org.jeecg.modules.renygjxl.service.IRenygjxlService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 人员轨迹线路
 * @Author: jeecg-boot
 * @Date:   2020-12-09
 * @Version: V1.0
 */
@Service
public class RenygjxlServiceImpl extends ServiceImpl<RenygjxlMapper, Renygjxl> implements IRenygjxlService {

	@Autowired
	private RenygjxlMapper renygjxlMapper;
	@Autowired
	private RenygjxlGuaidMapper renygjxlGuaidMapper;
	
	@Override
	@Transactional
	public void saveMain(Renygjxl renygjxl, List<RenygjxlGuaid> renygjxlGuaidList) {
		renygjxlMapper.insert(renygjxl);
		if(renygjxlGuaidList!=null && renygjxlGuaidList.size()>0) {
			for(RenygjxlGuaid entity:renygjxlGuaidList) {
				//外键设置
				entity.setZhubid(renygjxl.getId());
				renygjxlGuaidMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(Renygjxl renygjxl,List<RenygjxlGuaid> renygjxlGuaidList) {
		renygjxlMapper.updateById(renygjxl);
		
		//1.先删除子表数据
		renygjxlGuaidMapper.deleteByMainId(renygjxl.getId());
		
		//2.子表数据重新插入
		if(renygjxlGuaidList!=null && renygjxlGuaidList.size()>0) {
			for(RenygjxlGuaid entity:renygjxlGuaidList) {
				//外键设置
				entity.setZhubid(renygjxl.getId());
				renygjxlGuaidMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		renygjxlGuaidMapper.deleteByMainId(id);
		renygjxlMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			renygjxlGuaidMapper.deleteByMainId(id.toString());
			renygjxlMapper.deleteById(id);
		}
	}
	
}
